<?php

namespace App\Service\Workflow\Type;

use App\Enums\Workflow\WorkflowEnum;
use App\Library\Rms\RmsShopApi;
use App\Models\Common\ConfigModel;
use App\Models\System\SystemNotifyCenterModel;
use App\Models\Workflow\WorkflowOrderLogModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\Renew\RenewOrderService;
use App\Service\System\SystemNotifyService;

class RenewTypeService extends WorkflowBaseTypeService
{
    public function finishNode(int $auditStatus = WorkflowOrderLogModel::AUDIT_DEFAULT)
    {
        // 下个节点
        // $this->workflowOrderNodeModel;

        // 审核通过、不通过
        if ($this->currentNode->businessType == WorkflowEnum::BUSINESS_TYPE_FIX_AUDIT) {

            // 被驳回
            if ($auditStatus == WorkflowOrderLogModel::AUDIT_NO) {

                // 驳回到内部人员
                $this->sendFs('【待审核通知】商家续签已驳回');
            } else {

                // 正常流程进入下一步
                // 督导
                if ($this->currentNode->handlerType == WorkflowEnum::HANDLER_TYPE_SUPERVISE) {
                    $this->sendFs('【待审核通知】商家续签申请督导待初审');
                }

                // 省总
                if ($this->currentNode->handlerType == WorkflowEnum::HANDLER_TYPE_PROVINCE) {
                    $this->sendFs('【待审核通知】商家续签申请省总待初审');
                }

                // 总部
                if ($this->currentNode->handlerType == WorkflowEnum::HANDLER_TYPE_HEAD) {
                    $this->sendFs('【待审核通知】商家续签申请待复审');
                }
            }
        }

        // 到加盟商提交
        if ($this->currentNode->businessType == WorkflowEnum::BUSINESS_TYPE_SUBMIT_APPLY) {

            // 被驳回到加盟商
            if ($auditStatus == WorkflowOrderLogModel::AUDIT_NO) {

                // 加盟商推送APP
                $title = '您有1家门店续签申请被驳回';
                $shop = (new RmsShopApi())->getShopByIds([$this->workflowOrderModel->shopId], [], true)['data']['shopList'][0] ?? [];
                $shopCode = $shop['shopCode'] ?? '';
                $shopName = $shop['name'] ?? '';

                $content = "亲爱的夸父伙伴，门店编号：{$shopCode}{$shopName}续签申请申请被驳回，请点击查看详情进行处理。感谢您的理解！";
                $this->appPush($title, $content, SystemNotifyCenterModel::IS_POPUP_ON);
            }
        }

        // 下一步 - 签约
        if ($this->currentNode->businessType == WorkflowEnum::BUSINESS_TYPE_RENEW_SIGN) {

            // 加盟商推送APP
            $title = '您有1家门店续签申请复审已通过';
            $shop = (new RmsShopApi())->getShopByIds([$this->workflowOrderModel->shopId], [], true)['data']['shopList'][0] ?? [];
            $shopCode = $shop['shopCode'] ?? '';
            $shopName = $shop['name'] ?? '';

            $content = "亲爱的夸父伙伴，门店编号：{$shopCode}{$shopName}续签申请复审已通过，请点击查看详情进行处理。感谢您的理解！";
            $this->appPush($title, $content, SystemNotifyCenterModel::IS_POPUP_OFF);
        }
    }

    private function sendFs(string $headerTitle)
    {
        $shopCode = (new RmsShopApi())->getShopByIds([$this->workflowOrderModel->shopId], [], true)['data']['shopList'][0]['shopCode'] ?? 0;

        $appid = ConfigModel::getFsConfigByPlatformNameOrgId('openShop', 0)['appId'] ?? '';
        $path  = urlencode("pages/renewal/detail?workflowNo={$this->workflowOrderModel->workflowNo}");
        $url   = "https://applink.feishu.cn/client/mini_program/open?appId={$appid}&mode=window-semi&relaunch=true&path={$path}";

        // 员工 - 发送飞书
        RenewOrderService::flowNodeFsMsgSend(
            $shopCode,
            $this->currentNode->handlerUid,
            $this->workflowOrderModel->workflowNo,
            $headerTitle,
            $url,
            WorkflowEnum::HANDLER_TYPE_TEXT[$this->currentNode->handlerType] ?? '',
            date('Y-m-d'),
        );
    }

    private function appPush(string $title, string $content, int $isPopup = SystemNotifyCenterModel::IS_POPUP_ON)
    {
        $shopId = $this->workflowOrderModel->shopId;

        $users = RmsUserRemote::getShopManagerUserByShopId([$shopId], [], false)['lists'] ?? [];//通过shopId 获得对应店长的 userId
        $userIds = array_column($users, 'userId');

        $notify = SystemNotifyService::save(
            false,
            $title,
            SystemNotifyCenterModel::NOTIFY_TYPE_3,
            SystemNotifyCenterModel::NOTIFY_TARGET_1,
            '',
            $title,
            config('webUrl.notify_web_cdn_url') . "#/pages/work-order/renewal/renewal?workflowNo={$this->workflowOrderModel->workflowNo}",
            SystemNotifyCenterModel::METHOD_TYPE_AUTOMATIC,
            $content,
            '',
            $isPopup,
            1,
            $shopId
        );
        SystemNotifyService::recordLog($notify, '创建消息');
        SystemNotifyService::preSend($notify->id, $userIds);
    }
}
